import sys

from config.config import config
from utils.read import read_pkl

from pyecharts import options
from pyecharts.charts import Bar, Line, Grid

sys.path.append(config["project_path"])


def get_simi_min_max(data):
    """
    获取纵坐标轴simi的显示范围
    :param data:
    :return:
    """
    numsMin = []
    numsMax = []
    for col in data.columns[3:-2]:
        numsMin.append(data[col].min())
        numsMax.append(data[col].max())
    return min(numsMin), max(numsMax)


def graph_single(data, t):
    x = list(data["group"].values)
    # countSum = data["count"].sum()
    yMin, yMax = get_simi_min_max(data)
    yMin = yMin if data["min"].min() >= yMin else data["min"].min()
    line = (
        Line()
            .add_xaxis(x)
            .add_yaxis("min", list(data["min"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            .add_yaxis("1", list(data["1"].values), label_opts=options.LabelOpts(is_show=False),
                       is_smooth=True)  # 线上不显示数据
            .add_yaxis("2", list(data["2"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            .add_yaxis("3", list(data["3"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            .add_yaxis("4", list(data["4"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            .add_yaxis("5", list(data["5"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            .add_yaxis("6", list(data["6"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            .add_yaxis("7", list(data["7"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            .add_yaxis("8", list(data["8"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            .add_yaxis("9", list(data["9"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            .add_yaxis("10", list(data["10"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            .set_global_opts(
            title_opts=options.TitleOpts(
                title="Single-%s" % t,
            ),
            xaxis_opts=options.AxisOpts(
                type_="category",
                boundary_gap=False,
                axispointer_opts=options.AxisPointerOpts(is_show=True, type_="shadow"),
            ),
            yaxis_opts=options.AxisOpts(
                type_="value",
                min_=yMin,
                max_=yMax,
                axistick_opts=options.AxisTickOpts(is_show=True),
                splitline_opts=options.SplitLineOpts(is_show=True),
            ),
            datazoom_opts=options.DataZoomOpts(xaxis_index=[0, 1])
        )
    )
    bar = (
        Bar()
            .add_xaxis(x)
            .add_yaxis("count", list(data['count'].values),  # [i / countSum for i in list(data['count'].values)]
                       label_opts=options.LabelOpts(is_show=False))
            .set_global_opts(
            title_opts=options.TitleOpts(
                title="Single-%s" % t,
                pos_top="50%"
            ),
            legend_opts=options.LegendOpts(pos_top="50%"),
            xaxis_opts=options.AxisOpts(
                type_="category",
                boundary_gap=False,
                axispointer_opts=options.AxisPointerOpts(is_show=True, type_="shadow"),
            ),
            yaxis_opts=options.AxisOpts(
                type_="value",
                axistick_opts=options.AxisTickOpts(is_show=True),
                splitline_opts=options.SplitLineOpts(is_show=True),
            )
        )
    )

    grid = (
        Grid(init_opts=options.InitOpts(width="1880px", height="880px"))
            .add(line, grid_opts=options.GridOpts(pos_bottom="55%"))
            .add(bar, grid_opts=options.GridOpts(pos_top="55%"))
            .render('single_%s.html' % t)
    )


def graph_multi(data, t):
    x = list(data["group"].values)
    # countSum = data["count"].sum()
    yMin, yMax = get_simi_min_max(data)
    yMax = yMax if data["max"].max() <= yMax else data["max"].max()
    line = (
        Line()
            .add_xaxis(x)
            .add_yaxis("99.9", list(data["99.9"].values), label_opts=options.LabelOpts(is_show=False),
                       is_smooth=True)  # 线上不显示数据
            # .add_yaxis("99.91", list(data["99.91"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            # .add_yaxis("99.92", list(data["99.92"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            # .add_yaxis("99.93", list(data["99.93"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            # .add_yaxis("99.94", list(data["99.94"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            # .add_yaxis("99.95", list(data["99.95"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            # .add_yaxis("99.96", list(data["99.96"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            # .add_yaxis("99.97", list(data["99.97"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            # .add_yaxis("99.98", list(data["99.98"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            .add_yaxis("99.99", list(data["99.99"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            .add_yaxis("99.999", list(data["99.999"].values), label_opts=options.LabelOpts(is_show=False),
                       is_smooth=True)
            .add_yaxis("99.9999", list(data["99.9999"].values), label_opts=options.LabelOpts(is_show=False),
                       is_smooth=True)
            .add_yaxis("99.99999", list(data["99.99999"].values), label_opts=options.LabelOpts(is_show=False),
                       is_smooth=True)
            .add_yaxis("max", list(data["max"].values), label_opts=options.LabelOpts(is_show=False), is_smooth=True)
            .set_global_opts(
            title_opts=options.TitleOpts(
                title="Multi-%s" % t,
            ),
            xaxis_opts=options.AxisOpts(
                type_="category",
                boundary_gap=False,
                axispointer_opts=options.AxisPointerOpts(is_show=True, type_="shadow"),
            ),
            yaxis_opts=options.AxisOpts(
                type_="value",
                min_=yMin,
                max_=yMax,
                axistick_opts=options.AxisTickOpts(is_show=True),
                splitline_opts=options.SplitLineOpts(is_show=True),
            ),
            datazoom_opts=options.DataZoomOpts(xaxis_index=[0, 1])
        )
    )
    bar = (
        Bar()
            .add_xaxis(x)
            .add_yaxis("count", list(data['count'].values),  # [i / countSum for i in list(data['count'].values)]
                       label_opts=options.LabelOpts(is_show=False))
            .set_global_opts(
            title_opts=options.TitleOpts(
                title="Multi-%s" % t,
                pos_top="50%"
            ),
            legend_opts=options.LegendOpts(pos_top="50%"),
            xaxis_opts=options.AxisOpts(
                type_="category",
                boundary_gap=False,
                axispointer_opts=options.AxisPointerOpts(is_show=True, type_="shadow"),
            ),
            yaxis_opts=options.AxisOpts(
                type_="value",
                axistick_opts=options.AxisTickOpts(is_show=True),
                splitline_opts=options.SplitLineOpts(is_show=True),
            ),
            datazoom_opts=options.DataZoomOpts(xaxis_index=[0, 1])
        )
    )

    grid = (
        Grid(init_opts=options.InitOpts(width="1880px", height="880px"))
            .add(line, grid_opts=options.GridOpts(pos_bottom="55%"))
            .add(bar, grid_opts=options.GridOpts(pos_top="55%"))
            .render('Multi_%s.html' % t)
    )


if __name__ == '__main__':
    # 可视化统计结果
    graph_single(read_pkl("graph/data/graph_single_T.pkl"), "T")
    graph_single(read_pkl("graph/data/graph_single_F.pkl"), "F")
    graph_single(read_pkl("graph/data/graph_single_TF.pkl"), "TF")

    graph_multi(read_pkl("graph/data/graph_multi_T_all.pkl"), "T")
    graph_multi(read_pkl("graph/data/graph_multi_F_all.pkl"), "F")
    graph_multi(read_pkl("graph/data/graph_multi_TF_all.pkl"), "TF")
